package com.loe.ui.quickjs

import android.os.Build
import com.loe.logger.LoeLogger
import com.loe.mvp.ext_app.px
import com.loe.mvp.ext_java.delaySafe
import com.loe.mvp.ext_java.safe
import com.loe.mvp.ext_java.timer
import com.loe.ui.LoeUI
import com.loe.ui.dp
import com.loe.ui.registerFun
import com.loe.ui.registerFunReturn
import com.quickjs.*
import kotlin.concurrent.thread
import kotlin.system.exitProcess

/** 初始化js API */
fun JSContext.initAPI(): JSContext
{
    // 初始化常量
    initConst()

    // 状态栏高度
    val resourceId = LoeUI.app.resources.getIdentifier("status_bar_height", "dimen", "android")
    val statusHeight = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && resourceId > 0)
    {
        dp(LoeUI.app.resources.getDimensionPixelSize(resourceId))
    } else
    {
        0
    }
    set("statusHeight", statusHeight)

    // dp转px
    registerFunReturn("px")
    {
        return@registerFunReturn px(it.getDouble(0))
    }
    // 打印
    registerFun("log")
    {
        val o = it.get(0)
        when (o)
        {
            is JSArray ->
            {
                LoeLogger.d(o.toJSONArray().toString(4))
            }
            is JSObject ->
            {
                LoeLogger.d(o.toJSONObject().toString(4))
            }
            else ->
            {
                LoeLogger.d(o.toString())
            }
        }
    }
    // 定时器
    registerFunReturn("setTimer")
    {
        val callback = it.getObject(0) as JSFunction
        val interval = it.getInteger(1).toLong()
        var times = it.getInteger(2).toLong()
        if (times == 0L) times = Long.MAX_VALUE

        val task = timer(interval, interval, times)
        {
            safe { callback.call(this, JSArray(context).push(it.toInt())) }
        }
        return@registerFunReturn JSObject(context).also()
        {
            it.registerFun("pause")
            {
                task.pause()
            }
            it.registerFun("resume")
            {
                task.resume()
            }
            it.registerFun("stop")
            {
                task.finish()
            }
            it.registerFun("start")
            {
                task.start()
            }
        }

    }
    // 延时器
    registerFunReturn("setDelay")
    {
        val callback = it.getObject(0) as JSFunction
        val delay = it.getInteger(1).toLong()

        val task = delaySafe(delay)
        {
            callback.call(this, null)
        }
        return@registerFunReturn JSObject(context).also()
        {
            it.registerFun("stop")
            {
                task.stop()
            }
            it.registerFun("start")
            {
                task.start()
            }
        }
    }
    // 退出进程
    registerFun("exitApp")
    {
        thread()
        {
            if(it.length() > 0)
            {
                Thread.sleep(it.getInteger(0).toLong())
            }else
            {
                Thread.sleep(800)
            }
            exitProcess(0)
        }
    }
    // 网络请求
    initLink()
    // 选择器
    initSelector()

    return this
}